package com.fcx.workai.jinchang.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fcx.workai.jinchang.R;
import com.fcx.workai.jinchang.ui.main.MainActivity;
import com.hxh.component.basicore.util.Utils;
import com.hxh.component.business.app.App;
import com.hxh.component.business.arouter.MyAppProvider;
import com.hxh.component.business.utils.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * author mardawang
 * <p>
 * email:wy363681759@163.com
 * <p>
 * date: 2018/5/28
 * <p>
 * desc:
 */

public class GuideActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{


    private ViewPager vp;
    private int []imageIdArray;//图片资源的数组
    private List<View> viewList;//图片资源的集合
    private ViewGroup vg;//放置圆点

    //最后一页的按钮
    private TextView ib_start;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_guide);

        Utils.SP.editor().putBoolean(Constant.SpTag.IS_FIRST_START,false).commit();
        ib_start = (TextView) findViewById(R.id.guide_ib_start);
        ib_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isnouser = App.getUserDTO().getUser()!=null?false:true;
                if(isnouser)
                {
                    MyAppProvider.startLoginActivty(Constant.RouterTag.SHOWTAG, Constant.RouterTag.LOGIN_SHOWTYPE_SMSLOGIN);
                }else {
                    startActivity(new Intent(GuideActivity.this,MainActivity.class));
                }
                GuideActivity.this.finish();
            }
        });

        //加载ViewPager
        initViewPager();

        //加载底部圆点
//        initPoint();
    }

    /**
     * 加载底部圆点
     */
//    private void initPoint() {
//        //这里实例化LinearLayout
//        vg = (ViewGroup) findViewById(R.id.guide_ll_point);
//        //根据ViewPager的item数量实例化数组
//        ivPointArray = new ImageView[viewList.size()];
//        //循环新建底部圆点ImageView，将生成的ImageView保存到数组中
//        int size = viewList.size();
//        for (int i = 0;i<size;i++){
//            iv_point = new ImageView(this);
//            iv_point.setLayoutParams(new ViewGroup.LayoutParams(20,20));
//            iv_point.setPadding(30,0,30,0);//left,top,right,bottom
//            ivPointArray[i] = iv_point;
//            //第一个页面需要设置为选中状态，这里采用两张不同的图片
//            if (i == 0){
//                iv_point.setBackgroundResource(R.mipmap.full_holo);
//            }else{
//                iv_point.setBackgroundResource(R.mipmap.empty_holo);
//            }
//            //将数组中的ImageView加入到ViewGroup
//            vg.addView(ivPointArray[i]);
//        }
//
//    }

    /**
     * 加载图片ViewPager
     */
    private void initViewPager() {
        vp = (ViewPager) findViewById(R.id.guide_vp);
        //实例化图片资源
        imageIdArray = new int[]{R.drawable.icon_guide01,R.drawable.icon_guide02,R.drawable.icon_guide03};
        viewList = new ArrayList<>();
        //获取一个Layout参数，设置为全屏
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);

        //循环创建View并加入到集合中
        int len = imageIdArray.length;
        for (int i = 0;i<len;i++){
            //new ImageView并设置全屏和图片资源
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setBackgroundResource(imageIdArray[i]);

            //将ImageView加入到集合中
            viewList.add(imageView);
        }

        //View集合初始化好后，设置Adapter
        vp.setAdapter(new GuidePageAdapter(viewList));
        //设置滑动监听
        vp.setOnPageChangeListener(this);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /**
     * 滑动后的监听
     * @param position
     */
    @Override
    public void onPageSelected(int position) {
        //循环设置当前页的标记图
//        int length = imageIdArray.length;
//        for (int i = 0;i<length;i++){
//            ivPointArray[position].setBackgroundResource(R.mipmap.full_holo);
//            if (position != i){
//                ivPointArray[i].setBackgroundResource(R.mipmap.empty_holo);
//            }
//        }

        //判断是否是最后一页，若是则显示按钮
        if (position == imageIdArray.length - 1){
//            ib_start.setVisibility(View.VISIBLE);
//        }else {
//            ib_start.setVisibility(View.GONE);
        }
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }

    class GuidePageAdapter extends PagerAdapter {

        private List<View> viewList;

        public GuidePageAdapter(List<View> viewList) {
            this.viewList = viewList;
        }

        /**
         * @return 返回页面的个数
         */
        @Override
        public int getCount() {
            if (viewList != null){
                return viewList.size();
            }
            return 0;
        }

        /**
         * 判断对象是否生成界面
         * @param view
         * @param object
         * @return
         */
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        /**
         * 初始化position位置的界面
         * @param container
         * @param position
         * @return
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            if(position==viewList.size()-1){
                viewList.get(position).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isnouser = App.getUserDTO().getUser()!=null?false:true;
                        if(isnouser)
                        {
                            MyAppProvider.startLoginActivty(Constant.RouterTag.SHOWTAG, Constant.RouterTag.LOGIN_SHOWTYPE_SMSLOGIN);
                        }else {
                            startActivity(new Intent(GuideActivity.this,MainActivity.class));
                        }
                        GuideActivity.this.finish();
                    }
                });
            }

            return viewList.get(position);
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));
        }
    }
}
